<template>
  <div class="app-container">
    <!-- 查询参数及按钮部分 -->
    <div class="button">
      <div class="filter-container">
        <el-button
          size="small"
          class="filter-item"
          type="primary"
          @click="handleEdit"
        >
          修改
        </el-button>
        <el-button
          size="small"
          class="filter-item"
          type="primary"
          @click="getList"
        >
          取消修改
        </el-button>
        <el-button
          size="small"
          class="filter-item"
          type="primary"
          :loading="saveBtnLoading"
          @click="handleSave"
        >
          保存
        </el-button>
      </div>
    </div>
    <!-- 表格部分 -->
    <el-table
      ref="singalTable"
      v-loading="listLoading"
      :data="list"
      :stripe="true"
      :height="tableHeight"
      fit
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column
        type="selection"
        align="center"
        width="55"
      />
      <el-table-column
        type="index"
        align="center"
        width="80"
        label="序号"
      />
      <el-table-column
        label="省级工资项名称"
        align="center"
      >
        <template slot-scope="{row}">
          <span>{{ row.baseSalaryName }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="对照项"
        align="center"
      >
        <template slot-scope="{row}">
          <div v-if="row.isOperate" style="width:600px">
            <el-select
              v-model="row.citySalaryCodeArray"
              multiple
              placeholder="请选择"
              style="width:600px"
              @change="bindSelectChange()"
              filterable
            >
              <el-option
                v-for="item in localList"
                :key="item.salaryCode"
                :label="item.salaryName"
                :value="item.salaryCode"
                :disabled="item.disabled"
              />
            </el-select>
          </div>
          <span v-else>{{ row.citySalaryNameList }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!--分页部分-->
    <!-- <pagination
      :pageSizes="pageSizes"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    /> -->
  </div>
</template>

<script>
import {
  querysalaryBaseRelevance,
  submitData
} from '@/api/salaryConfigManage/salaryBaseRelevance'
import {
  queryPage
} from '@/api/salaryConfigManage/salaryProjectMaintenance'
import Pagination from '@/components/Pagination'
import tableEmpty from '@/components/table/empty'

export default {
  name: 'salaryBaseRelevance',
  components: {
    Pagination,
    tableEmpty
  },
  filters: {
    dictFilter(value, dictType, that) {
      const dict = that.dict[dictType]
      for (const item of dict) {
        if (value === item.value) {
          return item.label
        }
      }
    }
  },
  data() {
    return {
      tableHeight: null,
      that: this,
      list: [],
      localList: [],
      submitIds:[],
      submitList:[],
      total: 0,
      listLoading: true,
      saveBtnLoading: false,
      pageSizes: [ 100, 200, 500, 2000, 5000, 10000],
      listQuery: {
        page: 1,
        limit: 200
      },
      localListQuery: {
        page: 1,
        limit: 200
      }
    }
  },
  computed: {
    
  },
  watch: {
    list(val) {
      this.doLayout()
    }
  },
  created() {
    this.getList()
  },
  mounted() {
    this.getTbHeight()
  },
  beforeMount() {
    window.addEventListener('resize', this.getTbHeight)
  },

  methods: {
    /* 重新渲染table组件 */
    doLayout() {
      this.$nextTick(() => {
        this.$refs.singalTable.doLayout()
      })
    },
    // 获取table高度的方法
    getTbHeight() {
      this.tableHeight = window.innerHeight - 204
    },
    handleEdit() {
      const selection = this.$refs.singalTable.selection
      if (selection.length === 0) {
        this.$message({
          message: '请选择数据',
          type: 'warning',
          showClose: true,
          duration: 4000
        })
        return
      }
      selection.forEach(item => {
      // 避免多次点击编辑按钮，导致出现重复数据
        if(item.isShow === null|| item.isShow === ''){
           item.isShow=1
        }
        this.$set(item, 'isOperate', true)
      })
    },
    handleSave() {
      this.saveBtnLoading = true
      this.list.forEach(item =>{
       let rowchose= this.localList.filter(item2 =>{
            if(item.citySalaryCodeArray){
              return item.citySalaryCodeArray.includes(item2.salaryCode)
            }else{
              return false
            }
          })
          item.citySalaryNameArray =rowchose.map(item3 =>item3.salaryName)
      })
      submitData(this.list)
        .then(response => {
          this.$message({
            message: '保存成功',
            type: 'success',
            showClose: true,
            duration: 2000
          })
          this.getList()
          this.saveBtnLoading = false
        })
        .catch(() => {
          this.saveBtnLoading = false
        })
    },
    bindSelectChange() {
      //  获取 当前 editListData 里的 code值
      const tempList = this.list.map(item => item.citySalaryCodeArray)
     const tempList2=[]
     tempList.forEach(item =>{
      if(item){
        item.forEach(item2 =>{
          tempList2.push(item2)
        })
      }
     })
      this.localList.forEach(item => {
        // 判断 当前code 里是否被选择 选择就不可再次选择,避免重复
        tempList2.includes(item.salaryCode) ? item.disabled = true : item.disabled = false
      })
    },
    getList() {
      this.listLoading = true
      querysalaryBaseRelevance(this.listQuery)
        .then(response => {
          this.list = response.data.rows
          this.total = response.data.total
          this.listLoading = false
          this.initData()
        })
        .catch(() => {
          this.initData()
        })
    },
    initData(){
      queryPage(this.localListQuery)
        .then(response => {
          this.localList = response.data.rows
          this.bindSelectChange()
        })
    }
  }
}
</script>

<style lang="scss" scoped>
// .app-title {
//   margin-bottom: 15px;
//   height: 20px;
//   border-bottom: 1px solid #dcdfe6;
// }

.button {
  float: right;
}

/** 文本框高度 **/
::v-deep .el-input {
  width: auto !important;
  height: auto !important;
}

</style>
